package com.hqj.util;

import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;
import org.apache.shiro.crypto.hash.Sha384Hash;

/**
 * shiro的密码验证及加密
 */
public class MyMatcher extends SimpleCredentialsMatcher {

    //密码的验证
    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {

        //获取登录的令牌(登录名与处理类组成的)
        UsernamePasswordToken UsernamePasswordToken = (UsernamePasswordToken)token;
        //从token中获取出密码(登录密码)
        String pwd = encrypt(String.valueOf(UsernamePasswordToken.getPassword()));
        //获取数据库的密码
        String password = (String)info.getCredentials();
        return equals(pwd,password);
    }

    //密码加密
    private String encrypt(String data){
        String dataHex = new Sha384Hash(data).toBase64();
        return dataHex;
    }

    public static void main(String[] args) {
        MyMatcher myMatcher = new MyMatcher();
        String npwd = myMatcher.encrypt("123456");
        System.out.println("加密后:"+npwd);
    }

}
